package cn.lghuntfor.commons.sms.aliyun.config;

import cn.hutool.core.util.StrUtil;
import cn.lghuntfor.commons.sms.aliyun.common.AliyunSmsConsts;
import cn.lghuntfor.commons.sms.aliyun.service.impl.AliyunSmsServiceImpl;
import cn.lghuntfor.commons.sms.api.config.SmsProperties;
import cn.lghuntfor.commons.sms.api.service.InheritSmsService;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云短信配置
 * @author liaogang
 * @date 2021/1/21 9:22
 */
@Configuration
@EnableConfigurationProperties(value = SmsProperties.class)
public class AliyunSmsConfig {

    @Autowired
    private SmsProperties smsProperties;

    @Value("${spring.sms.aliyun.accessKey:}")
    private String accessKey;

    @Value("${spring.sms.aliyun.accessSecret:}")
    private String accessSecret;

    @Bean(name = "ALIYUN")
    public InheritSmsService aliyunSmsService() {
        return new AliyunSmsServiceImpl();
    }

    @Bean
    public IAcsClient acsClient() {
        DefaultProfile profile;
        if (StrUtil.isAllNotBlank(accessKey, accessSecret)) {
            profile = DefaultProfile.getProfile(AliyunSmsConsts.ALIYUN_REGION_ID, accessKey, accessSecret);
        } else {
            profile = DefaultProfile.getProfile(AliyunSmsConsts.ALIYUN_REGION_ID, smsProperties.getAccessKey(), smsProperties.getAccessSecret());
        }
        return new DefaultAcsClient(profile);
    }

}
